import { Component, OnInit, Output, EventEmitter } from '@angular/core';

import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { InitService } from '../../../public/serve/init/init.service';

import { appContentUrl } from '../../content.url';
import { content } from '../../content.data';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import {Service} from '../../../server/server';

declare var $: any;

@Component({
  selector: 'app-content-product-add-author',
  templateUrl: './content-product-add-author.component.html'
})
export class ContentProductAddAuthorComponent implements OnInit {

  total: number = 0;
  naturalNum: number = 0;
  list: Array<object>;
  prodctAssignAuthor: string;

  isSelectAll: boolean = false;
  isChose: boolean = false;

  @Output() addAuthorPersons = new EventEmitter<string>();

  constructor(
    public initService: InitService,
    private request: CudTableService,
    private hintsService: HintsService,
    private server : Service,
    public removeTableService: RemoveTableService
    ) { }


  ngOnInit() {
  }

  getAuthorList() {
    let params = this.getTastAuditListParams();
    this.request.postRequest(this.server.baseUrl + appContentUrl.getAuthorList, params).then((res: any) => {
      if (res.result_code === '0000') {
        this.list = res.result;
        this.total = res.length;
        this.naturalNum = content.pagination.naturalNum;
        content.retrieval.author = '';
      } else {
        this.hintsService.messages(res.message, res.result_code);
      }
      return;
    })
      .catch((err: any) => {
        this.hintsService.messages("error", "error");
        return;
      });
  }

  getTastAuditListParams() {
    return {
      token: content.retrieval.token,
      pageNumber: content.pagination.pageNo,
      pageSize: content.pagination.pageSize,
      id: content.retrieval.id,
      queryType: content.retrieval.queryType
    }
  }

  /**
  * 全选
  * @param bool
  * @param tabVal
  */
  _selectAll(bool, tabVal) {
    this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'userName');
  }

  /**
   * 单选
   * @param bool
   * @param synonymId
   * @param length
   */
  _chose(bool, synonymId, length) {
    this.isSelectAll = this.removeTableService.removeChose(bool, synonymId, length)['bool'];
  }

  /**
   * 重置表格复选框、选中的ID
   */
  public _cancelChecked() {
    this.isChose = this.isSelectAll = false;
    this.removeTableService._clearId('userName');
  }

  public dismissModal() {
    this.prodctAssignAuthor = this.removeTableService.getRemoveTableId.toString();
    content.retrieval.author = this.prodctAssignAuthor;
    this.addAuthorPersons.emit(this.prodctAssignAuthor);
    $('#addAssignAuthor').modal('hide');
    this.isSelectAll = this.isChose = false;
  }

  /**
   * 取消初始化
   */
  choseInit(){
    content.retrieval.author = '';
    this.isSelectAll = this.isChose = false;
  }

}
